package com.cd120.unicorn.push;

import java.util.LinkedHashSet;
import java.util.Set;

import android.app.Activity;
import android.app.Notification;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;

import com.cd120.unicorn.R;

public class PushSetActivity extends Activity implements OnClickListener {

	Button mSetAlias;
	Button mSetPushTime;
	Button mSetTag;
	Button mStyleBasic;
	Button mStyleCustom;

	private void init() {
		mSetTag = (Button) findViewById(R.id.bt_tag);
		mSetAlias = (Button) findViewById(R.id.bt_alias);
		mStyleBasic = (Button) findViewById(R.id.setStyle1);
		mStyleCustom = (Button) findViewById(R.id.setStyle2);
		// mSetPushTime = (Button) findViewById(R.id.bu_setTime);
	}

	private void initListener() {
		mSetTag.setOnClickListener(this);
		mSetAlias.setOnClickListener(this);
		mStyleBasic.setOnClickListener(this);
		mStyleCustom.setOnClickListener(this);
		// mSetPushTime.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_tag:
			setTag();
			break;
		case R.id.bt_alias:
			setAlias();
			break;
		case R.id.setStyle1:
			setStyleBasic();
			break;
		case R.id.setStyle2:
			setStyleCustom();
			break;
		// case R.id.bu_setTime:
		// Intent intent = new Intent(PushSetActivity.this,
		// SettingActivity.class);
		// startActivity(intent);
		// break;
		}
	}

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.push_set_dialog);
		init();
		initListener();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 设置Alias
	 */
	private void setAlias() {
		EditText aliasEdit = (EditText) findViewById(R.id.et_alias);
		String alias = "userid1";
		SharedPreferences Tag_Alias = getSharedPreferences("Tag_Alias", 0);
		alias = Tag_Alias.getString("alias", alias);
		aliasEdit.setText(alias);
		alias = aliasEdit.getText().toString().trim();

		if (TextUtils.isEmpty(alias)) {
			Toast.makeText(PushSetActivity.this, R.string.error_alias_empty,
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (!ExampleUtil.isValidTagAndAlias(alias)) {
			Toast.makeText(PushSetActivity.this, R.string.error_tag_gs_empty,
					Toast.LENGTH_SHORT).show();
			return;
		}

		// 调用JPush API设置Alias
		JPushInterface.setAliasAndTags(getApplicationContext(), alias, null);
		Toast.makeText(PushSetActivity.this, R.string.setting_su,
				Toast.LENGTH_SHORT).show();
		// SharedPreferences Tag_Alias= getSharedPreferences("Tag_Alias", 0);
		Tag_Alias.edit().putString("alias", alias).commit();

	}

	/**
	 * 设置通知提示方式 - 基础属性
	 */
	private void setStyleBasic() {
		BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(
				PushSetActivity.this);
		builder.statusBarDrawable = R.drawable.ic_launcher;
		builder.notificationFlags = Notification.FLAG_AUTO_CANCEL; // 设置为点击后自动消失
		builder.notificationDefaults = Notification.DEFAULT_SOUND; // 设置为铃声（
																	// Notification.DEFAULT_SOUND）或者震动（
																	// Notification.DEFAULT_VIBRATE）
		JPushInterface.setPushNotificationBuilder(1, builder);
		Toast.makeText(PushSetActivity.this, "Basic Builder - 1",
				Toast.LENGTH_SHORT).show();
	}

	/**
	 * 设置通知栏样式 - 定义通知栏Layout
	 */
	private void setStyleCustom() {
		CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(
				PushSetActivity.this, R.layout.customer_notitfication_layout,
				R.id.icon, R.id.title, R.id.text);
		builder.layoutIconDrawable = R.drawable.ic_launcher;
		builder.developerArg0 = "developerArg2";
		JPushInterface.setPushNotificationBuilder(2, builder);
		Toast.makeText(PushSetActivity.this, "Custom Builder - 2",
				Toast.LENGTH_SHORT).show();
	}

	/**
	 * 设置TAG
	 */
	private void setTag() {
		// 检查 tag 的有效性
		EditText tagEdit = (EditText) findViewById(R.id.et_tag);
		String tag = tagEdit.getText().toString().trim();
		if (TextUtils.isEmpty(tag)) {
			Toast.makeText(PushSetActivity.this, R.string.error_tag_empty,
					Toast.LENGTH_SHORT).show();
			return;
		}

		// ","隔开的多个 转换成 Set
		String[] sArray = tag.split(",");
		Set<String> tagSet = new LinkedHashSet<String>();
		for (String sTagItme : sArray) {
			if (!ExampleUtil.isValidTagAndAlias(sTagItme)) {
				Toast.makeText(PushSetActivity.this,
						R.string.error_tag_gs_empty, Toast.LENGTH_SHORT).show();
				return;
			}
			tagSet.add(sTagItme);
		}

		// 调用JPush API设置Tag
		JPushInterface.setAliasAndTags(getApplicationContext(), null, tagSet);

		Toast.makeText(PushSetActivity.this, R.string.setting_su,
				Toast.LENGTH_SHORT).show();
	}

}